from jili.core.convert import str2datetime
limit2date=str2datetime("20200824")
def get_limit_rate(obj, d=None):
    # 300 :20200824
    r = 0.1
    # if obj[:3] == "002":
    #     if d:
    #         d = str2datetime(d)
    #         if d >= limit2date:
    #             r = 0.2
    #     else:
    #         r = 0.2
    if obj[:2] == "30":
        if d:
            d = str2datetime(d)
            if d >= limit2date:
                r = 0.2
        else:
            r = 0.2
    elif obj[:2] == "68":
        r = 0.2
    return r
def calc_uplimite_price(obj, price, d=None):
    r = get_limit_rate(obj, d)
    return round(price * (1 + r), 2)
def calc_downlimite_price(obj, price, d=None):
    r = get_limit_rate(obj, d)
    return round(price * (1 - r), 2)
def calc_updownlimite_price(obj, price, d=None):
    r = get_limit_rate(obj, d)
    return round(price * (1 + r), 2),round(price * (1 - r), 2)

def isuplimit_flag_4(b,r=0.09,preclose="preclose"):
    close = b["close"]
    up_price = b[preclose]*(1+r)
    if close>= up_price:
        r = 1
    else:
        r = 0
    return r
def isuplimit_flag_3(b,up_price=None,preclose="preclose"):
    timekey = b["timekey"]
    obj = b["obj"]
    close = b["close"]
    if up_price is None:
        up_price = calc_uplimite_price(obj, b[preclose], timekey)
    if close>= up_price:
        r = 1
    else:
        r = 0
    return r
def isuplimit_flag_2(b,up_price=None,preclose="preclose"):
    timekey = b["timekey"]
    obj = b["obj"]
    close = b["high"]
    if up_price   is None:
        up_price = calc_uplimite_price(obj, b[preclose], timekey)
    if close>= up_price and b["open"]<b["close"]:
        r = 1
    else:
        r = 0
    return r
def isuplimit_flag_1(b,up_price=None,preclose="preclose"):
    timekey = b["timekey"]
    obj = b["obj"]
    close = b["high"]
    if up_price   is None:
        up_price = calc_uplimite_price(obj, b[preclose], timekey)
    if close>= up_price:
        r = 1
    else:
        r = 0
    return r
def isdownlimit_flag_4(b,r=0.09,preclose="preclose"):
    close = b["close"]
    down_price = b[preclose]*(1-r)
    if close<= down_price:
        r = 1
    else:
        r = 0
    return r
def isdownlimit_flag_3(b,down_price=None,preclose="preclose"):
    timekey = b["timekey"]
    obj = b["obj"]
    close = b["close"]
    if down_price is None:
        down_price = calc_downlimite_price(obj, b[preclose], timekey)
    if close<= down_price:
        r = 1
    else:
        r = 0
    return r
def isdownlimit_flag_2(b,down_price=None,preclose="preclose"):
    timekey = b["timekey"]
    obj = b["obj"]
    close = b["high"]
    if down_price   is None:
        down_price = calc_downlimite_price(obj, b[preclose], timekey)
    if close<= down_price and b["open"]>b["close"]:
        r = 1
    else:
        r = 0
    return r
def isdownlimit_flag_1(b,down_price=None,preclose="preclose"):
    timekey = b["timekey"]
    obj = b["obj"]
    close = b["high"]
    if down_price   is None:
        down_price = calc_downlimite_price(obj, b[preclose], timekey)
    if close<= down_price:
        r = 1
    else:
        r = 0
    return r